package com.rzjm.exercise.module.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rzjm.exercise.common.base.service.impl.BaseServiceImpl;
import com.rzjm.exercise.common.utils.DateUtils;
import com.rzjm.exercise.common.utils.MathUtil;
import com.rzjm.exercise.module.dao.DeviceRangeResultDao;
import com.rzjm.exercise.module.domain.DeviceRange;
import com.rzjm.exercise.module.domain.DeviceRangeResult;
import com.rzjm.exercise.module.domain.WxUser;
import com.rzjm.exercise.module.service.DeviceRangeResultService;
import com.rzjm.exercise.sys.system.user.domain.UserDO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DeviceRangeResultServiceImpl extends BaseServiceImpl<DeviceRangeResultDao, DeviceRangeResult> implements DeviceRangeResultService {

    @Resource
    private DeviceRangeResultDao deviceRangeResultDao;

    @Override
    public Map<String, Object> getChartDataByDeviceRange(WxUser wxUser, UserDO user, DeviceRange deviceRange, String beginDate, String endDate) {
        Map<String,Object> mapReturn = new HashMap<>();

        List<String> times = new ArrayList<>();
        List<String> values = new ArrayList<>();

        QueryWrapper<DeviceRangeResult> qw = new QueryWrapper<>();
        qw.eq("device_num",deviceRange.getDeviceNum());
        if(user.getUserType() > 0){
            if(wxUser == null || (wxUser !=null && wxUser.getIsTest() == 0)){
                qw.eq("is_test", 0);
            }
        }
        if(StrUtil.isNotEmpty(beginDate)){
            qw.between("create_time", beginDate + " 00:00:00", endDate + " 23:59:59");
        }
        qw.orderByAsc("create_time");
        List<DeviceRangeResult> rangeRecords = deviceRangeResultDao.selectList(qw);
        for (DeviceRangeResult rangeRecord : rangeRecords){
            times.add(DateUtils.getSimpleTime1(rangeRecord.getCreateTime()));
            values.add(MathUtil.getDouble((rangeRecord.getWeight() / 1000)) +"");
        }

        mapReturn.put("node_name",deviceRange.getDeviceNum());
        mapReturn.put("data_title", "剩余重量");
        mapReturn.put("data_unit", "Kg");

        mapReturn.put("canvasImg","");
        mapReturn.put("times",times);
        mapReturn.put("values",values);
        mapReturn.put("count",times.size());
        mapReturn.put("maxValue",deviceRange.getStoreHeight());
        mapReturn.put("minValue",0);

        return mapReturn;
    }
}
